package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.*;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.pojo.dto.SaveLeveragePlanDTO;
import com.hamster.crmpre.service.LeveragePlanService;
import com.hamster.crmpre.service.PhoneUserService;
import com.hamster.crmpre.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/28 11:50
 * @description：杠杆计划服务实现类
 * @modified By：
 * @version:
 */
@Service
public class LeveragePlanServiceImpl implements LeveragePlanService {

    @Autowired
    private LeverageMapper leverageMapper;
    @Autowired
    private LeveragePlanInformationMapper leveragePlanInformationMapper;
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private CompanyAccountMapper companyAccountMapper;
    @Autowired
    private LeverageConfigMapper leverageConfigMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeePerformanceMapper employeePerformanceMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private PhoneUserService phoneUserService;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 根据id返回一条杠杆信息
     * @param id 杠杆信息唯一标识id
     * @return 一条杠杆信息
     */
    @Override
    public Leverage getLeverage(String id) {
        return leverageMapper.selectById(id);
    }

    /**
     * 添加一条杠杆计划信息，给会计发信息
     * @param leveragePlanInformation
     */
    @Transactional
    @Override
    public void insert(LeveragePlanInformation leveragePlanInformation) {
        Date date = new Date();
        leveragePlanInformation.setEmployeeId(LoginInterceptor.getLoginUser().getUserId());//员工id
        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
        leveragePlanInformation.setEmployeeName(userName);//员工姓名
        Customer customer = customerMapper.selectById(leveragePlanInformation.getCustomerId());
        leveragePlanInformation.setCustomerPhone(customer.getPhone());//客户手机号
        leveragePlanInformation.setCustomerName(customer.getName());//客户姓名
        leveragePlanInformation.setLeverageTitle("本金:"+leveragePlanInformation.getPrincipal()+","+leveragePlanInformation.getTargetAmount());//杠杆计划标题

        leveragePlanInformation.setActualAmount(0.0);//实际金额
        leveragePlanInformation.setCost(0.0);//成本
        leveragePlanInformation.setCreateTime(date);
        leveragePlanInformation.setVerify("0");//会计审核  0 待审  1已通过   2拒绝
        leveragePlanInformation.setInputUser(userName);
        leveragePlanInformation.setInputTime(date);
        leveragePlanInformation.setDel("0");
        leveragePlanInformation.setState("0");
        leveragePlanInformationMapper.insert(leveragePlanInformation);
        //给会计发信息
        PersonalMessage personalMessage = new PersonalMessage();
        personalMessage.setCreateTime(date);
        personalMessage.setReceiverId("财务部");
        personalMessage.setReceiverName("财务部");
        personalMessage.setDepartment("财务部");
        personalMessage.setMessageLink(leveragePlanInformation.getId());//详情的id
        personalMessage.setState("0");//未读
        personalMessage.setMessageType("发起杠杆计划");//任务类型
        personalMessage.setSenderId(LoginInterceptor.getLoginUser().getUserId());//发送人id
        personalMessage.setSenderName(userName);//发送人姓名
        personalMessage.setInputTime(date);
        personalMessage.setInputUser(userName);

        personalMessageMapper.insert(personalMessage);//发送消息
    }

    /**
     * 根据id查询杠杆计划
     * @param messageLink
     * @return
     */
    @Override
    public LeveragePlanInformation initiateLeveragePlansView(String messageLink) {
        return leveragePlanInformationMapper.selectById(messageLink);
    }

    /**
     * 发起杠杆计划审核通过
     * @param messageLink
     */
    @Override
    public void initiateLeveragePlansPass(String messageLink) {
        //1.会计审核  0 待审  1已通过   2拒绝
        leveragePlanInformationMapper.initiateLeveragePlansPass(messageLink);
        LeveragePlanInformation leveragePlanInformation = leveragePlanInformationMapper.selectById(messageLink);
        //2.系统总账增加
        CompanyAccount companyAccount = new CompanyAccount();
        companyAccount.setMoney(leveragePlanInformation.getPrincipal());//总账+杠杆本金
        companyAccount.setCreateTime(new Date());
        companyAccount.setTitle("发起杠杆计划");
        companyAccount.setContent("客户:"+leveragePlanInformation.getCustomerName()+","
        +leveragePlanInformation.getCustomerPhone()+" 发起杠杆计划；本金："+leveragePlanInformation.getPrincipal()
        +"，目标金额："+leveragePlanInformation.getTargetAmount());
        companyAccountMapper.insert(companyAccount);
    }

    /**
     * 发起杠杆计划审核拒绝
     * @param messageLink
     */
    @Override
    public void initiateLeveragePlansRefuse(String messageLink) {
        //会计审核  0 待审  1已通过   2拒绝
        leveragePlanInformationMapper.initiateLeveragePlansRefuse(messageLink);
    }

    /**
     * 提交保存杠杆计划，判断是否达标，并进行对应的操作
     * @param saveLeveragePlanDTO
     */
    @Override
    @Transactional
    public void saveLeveragePlan(SaveLeveragePlanDTO saveLeveragePlanDTO) {
        //根据id查询该计划的目标金额，然后拿实际金额做对比
        LeveragePlanInformation leveragePlanInformation = leveragePlanInformationMapper.selectById(saveLeveragePlanDTO.getId());
        leveragePlanInformation.setActualAmount(saveLeveragePlanDTO.getActualAmount());
        leveragePlanInformation.setCost(saveLeveragePlanDTO.getCost());
        leveragePlanInformationMapper.updateById(leveragePlanInformation);//更新实际金额 和 成本

        //完成百分比
        Double percentage = leveragePlanInformation.getActualAmount() / leveragePlanInformation.getTargetAmount();
        //不达标则不继续
        if (percentage < 1){
            return;
        }
        //查询该杠杆计划 对象
        String leverageType = leveragePlanInformation.getLeverageType();//杠杆计划的id
        Leverage leverage = leverageMapper.selectById(leverageType);//杠杆计划对象


        //3.如果已完成，设置状态和完成进度
        if (percentage == 1){
            //1.查询各组提成比例
            LeverageConfig leverageConfig = leverageConfigMapper.selectById("1");
            //2.查询对应各组的人员，和 对应的 提成比例
            //（本金 - 成本） *  百分比
            Double qualified = leverage.getQualified();
            Double money = (leveragePlanInformation.getPrincipal() - leveragePlanInformation.getCost()) * qualified;
            //原始服务经理id、[json数组]
            List<String> originalManagerList = JsonUtils.parseList(leveragePlanInformation.getOriginalManager(), String.class);
            Double  originalManagerMoney =money * leverageConfig.getOriginalPercent() / originalManagerList.size();
            for (String id : originalManagerList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(originalManagerMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-originalManagerMoney);//收入金额
                companyAccount.setContent("原始服务经理："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+originalManagerMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //微信组-总负责人id、[json数组]
            Double  wxGroupMoney =money * leverageConfig.getWxGroupPercent();
            List<String> wxGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getWxGroupResponsible(), String.class);
            Double  wxGroupResponsibleMoney =wxGroupMoney * leverageConfig.getWxGroupPrimary() / wxGroupResponsibleList.size();
            for (String id : wxGroupResponsibleList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(wxGroupResponsibleMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-wxGroupResponsibleMoney);//收入金额
                companyAccount.setContent("微信组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupResponsibleMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }


            //微信组-参与人id、[json数组]
            List<String> wxGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getWxGroupParticipate(), String.class);
            Double  wxGroupParticipateMoney =wxGroupMoney * leverageConfig.getWxGroupSecondary() / wxGroupParticipateList.size();
            for (String id : wxGroupParticipateList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(wxGroupParticipateMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-wxGroupParticipateMoney);//收入金额
                companyAccount.setContent("微信组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupParticipateMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }


            //落地组-总负责人id、[json数组]
            Double performGroupMoney = money * leverageConfig.getPerformGroup();
            List<String> performGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupResponsible(), String.class);
            Double  performGroupResponsibleMoney =performGroupMoney * leverageConfig.getPerformGroupPrimary() / performGroupResponsibleList.size();
            for (String id : performGroupResponsibleList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(performGroupResponsibleMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-performGroupResponsibleMoney);//收入金额
                companyAccount.setContent("落地组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupResponsibleMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //落地组-讲师id、[json数组]
            List<String> performGroupLecturerList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupLecturer(), String.class);
            Double  performGroupLecturerMoney =performGroupMoney * leverageConfig.getPerformGroupTeacher() / performGroupLecturerList.size();
            for (String id : performGroupLecturerList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(performGroupLecturerMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-performGroupLecturerMoney);//收入金额
                companyAccount.setContent("落地组讲师："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupLecturerMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //落地组-参与人id、[json数组]
            List<String> performGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupParticipate(), String.class);
            Double  performGroupParticipateMoney =performGroupMoney * leverageConfig.getPerformGroupSecondary() / performGroupParticipateList.size();
            for (String id : performGroupParticipateList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(performGroupParticipateMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-performGroupParticipateMoney);//收入金额
                companyAccount.setContent("落地组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupParticipateMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //保障组参与人id、[json数组]
            List<String> securityGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getSecurityGroupParticipate(), String.class);
            Double securityGroupParticipateMoney =money * leverageConfig.getSecurityGroup() / securityGroupParticipateList.size();
            for (String id : securityGroupParticipateList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(securityGroupParticipateMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-securityGroupParticipateMoney);//收入金额
                companyAccount.setContent("保障组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+securityGroupParticipateMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }





            //完成进度  0 未达到80%   1 80%-100%   2 100%   3 100%+
            leveragePlanInformationMapper.updateCompleteSchedule(saveLeveragePlanDTO.getId(),"2");
            leveragePlanInformationMapper.updateState(saveLeveragePlanDTO.getId(),"1");
            //如果正好100% 不做操作
            //通知财务一声
            Notice notice = new Notice();
            notice.setContent("客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone()+
                    "   杠杆计划："+leveragePlanInformation.getLeverageTitle()+"已完成100%");
            noticeMapper.insert(notice);

            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setMessageType("通知消息");
            personalMessage.setSenderId("系统");
            personalMessage.setSenderName("系统");
            personalMessage.setMessageLink(notice.getId());
            personalMessage.setCreateTime(new Date());
            personalMessage.setState("0");//消息状态 0未读
            personalMessage.setReceiverId("财务部");
            personalMessage.setReceiverName("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);

        }else if (percentage > 1){

            //1.查询各组提成比例
            LeverageConfig leverageConfig = leverageConfigMapper.selectById("1");


            //2.查询对应各组的人员，和 对应的 提成比例
            //（（本金 - 成本） *  百分比 ）  +  （ 额外完成 *  比例）
            Double beyond = leverage.getBeyond();//100%+ 比例
            Double excessPercentage = leverage.getExcessPercentage();//额外的比例
            Double moreMoney = leveragePlanInformation.getActualAmount() - leveragePlanInformation.getTargetAmount(); //额外金额
            Double money = ((leveragePlanInformation.getPrincipal() - leveragePlanInformation.getCost()) * beyond) +  (moreMoney * excessPercentage);
            //原始服务经理id、[json数组]
            List<String> originalManagerList = JsonUtils.parseList(leveragePlanInformation.getOriginalManager(), String.class);
            Double  originalManagerMoney =money * leverageConfig.getOriginalPercent() / originalManagerList.size();
            for (String id : originalManagerList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(originalManagerMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-originalManagerMoney);//收入金额
                companyAccount.setContent("原始服务经理："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+originalManagerMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //微信组-总负责人id、[json数组]
            Double  wxGroupMoney =money * leverageConfig.getWxGroupPercent();
            List<String> wxGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getWxGroupResponsible(), String.class);
            Double  wxGroupResponsibleMoney =wxGroupMoney * leverageConfig.getWxGroupPrimary() / wxGroupResponsibleList.size();
            for (String id : wxGroupResponsibleList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(wxGroupResponsibleMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-wxGroupResponsibleMoney);//收入金额
                companyAccount.setContent("微信组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupResponsibleMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }


            //微信组-参与人id、[json数组]
            List<String> wxGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getWxGroupParticipate(), String.class);
            Double  wxGroupParticipateMoney =wxGroupMoney * leverageConfig.getWxGroupSecondary() / wxGroupParticipateList.size();
            for (String id : wxGroupParticipateList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(wxGroupParticipateMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-wxGroupParticipateMoney);//收入金额
                companyAccount.setContent("微信组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+wxGroupParticipateMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }


            //落地组-总负责人id、[json数组]
            Double performGroupMoney = money * leverageConfig.getPerformGroup();
            List<String> performGroupResponsibleList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupResponsible(), String.class);
            Double  performGroupResponsibleMoney =performGroupMoney * leverageConfig.getPerformGroupPrimary() / performGroupResponsibleList.size();
            for (String id : performGroupResponsibleList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(performGroupResponsibleMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-performGroupResponsibleMoney);//收入金额
                companyAccount.setContent("落地组总负责人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupResponsibleMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //落地组-讲师id、[json数组]
            List<String> performGroupLecturerList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupLecturer(), String.class);
            Double  performGroupLecturerMoney =performGroupMoney * leverageConfig.getPerformGroupTeacher() / performGroupLecturerList.size();
            for (String id : performGroupLecturerList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(performGroupLecturerMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-performGroupLecturerMoney);//收入金额
                companyAccount.setContent("落地组讲师："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupLecturerMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //落地组-参与人id、[json数组]
            List<String> performGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getPerformGroupParticipate(), String.class);
            Double  performGroupParticipateMoney =performGroupMoney * leverageConfig.getPerformGroupSecondary() / performGroupParticipateList.size();
            for (String id : performGroupParticipateList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(performGroupParticipateMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-performGroupParticipateMoney);//收入金额
                companyAccount.setContent("落地组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+performGroupParticipateMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }

            //保障组参与人id、[json数组]
            List<String> securityGroupParticipateList = JsonUtils.parseList(leveragePlanInformation.getSecurityGroupParticipate(), String.class);
            Double securityGroupParticipateMoney =money * leverageConfig.getSecurityGroup() / securityGroupParticipateList.size();
            for (String id : securityGroupParticipateList) {
                Employee employee = employeeMapper.selectById(id);
                //添加员工业绩
                EmployeePerformance employeePerformance = new EmployeePerformance();
                employeePerformance.setEmployeeProfit(securityGroupParticipateMoney);//设置员工业绩提成
                employeePerformance.setEmployeeId(employee.getId());//员工id
                employeePerformance.setEmployeeName(employee.getName());//员工姓名
                employeePerformance.setCustomer(leveragePlanInformation.getCustomerName());//客户姓名
                employeePerformance.setCustomerId(leveragePlanInformation.getCustomerId());//客户id
                employeePerformance.setProjectId(leveragePlanInformation.getId());//购买项目id
                employeePerformance.setPurchaseItem(leveragePlanInformation.getLeverageTitle());//购买项目名称
                employeePerformance.setInputTime(new Date()); //业绩的录入日期
                employeePerformance.setType("2");  //0购买产品    1项目加盟  2杠杆计划
                employeePerformanceMapper.insert(employeePerformance);

                //给公司总账，添加一条明细记录
                CompanyAccount companyAccount = new CompanyAccount();
                companyAccount.setTitle("杠杆计划员工分成");            //公司明细标题
                companyAccount.setCreateTime(new Date());       //创建时间
                companyAccount.setMoney(-securityGroupParticipateMoney);//收入金额
                companyAccount.setContent("保障组参与人："+employee.getName()+","+employee.getPhone()+" 分得提成 ");//记账内容
                companyAccountMapper.insert(companyAccount);

                //给员工发一条通知
                Notice notice = new Notice();
                notice.setContent("杠杆计划："+leveragePlanInformation.getLeverageTitle()+"  杠杆计划分成："+securityGroupParticipateMoney);
                noticeMapper.insert(notice);

                PersonalMessage personalMessage = new PersonalMessage();
                personalMessage.setMessageType("通知消息");
                personalMessage.setSenderId("系统");
                personalMessage.setSenderName("系统");
                personalMessage.setMessageLink(notice.getId());
                personalMessage.setCreateTime(new Date());
                personalMessage.setState("0");//消息状态 0未读
                personalMessage.setReceiverId(employee.getId());
                personalMessage.setReceiverName(employee.getName());
                personalMessageMapper.insert(personalMessage);

            }



            leveragePlanInformationMapper.updateCompleteSchedule(saveLeveragePlanDTO.getId(),"3");
            leveragePlanInformationMapper.updateState(saveLeveragePlanDTO.getId(),"1");
            //高于100% 的金额归公司所有
            //给公司总账，添加一条明细记录
             Double profit = leveragePlanInformation.getActualAmount() - leveragePlanInformation.getTargetAmount();//额外收入
            CompanyAccount companyAccount = new CompanyAccount();
            companyAccount.setTitle("杠杆计划超100%完成");            //公司明细标题
            companyAccount.setCreateTime(new Date());       //创建时间
            companyAccount.setMoney(profit);//收入金额
            companyAccount.setContent("杠杆计划额外收入："+profit);//记账内容
            companyAccountMapper.insert(companyAccount);
            //通知财务一声
            //通知财务一声
            Notice notice = new Notice();
            notice.setContent("客户："+leveragePlanInformation.getCustomerName()+","+leveragePlanInformation.getCustomerPhone()+
                    "   杠杆计划："+leveragePlanInformation.getLeverageTitle()+"超额完成100%+   超额 "+profit+" 元");
            noticeMapper.insert(notice);

            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setMessageType("通知消息");
            personalMessage.setSenderId("系统");
            personalMessage.setSenderName("系统");
            personalMessage.setMessageLink(notice.getId());
            personalMessage.setCreateTime(new Date());
            personalMessage.setState("0");//消息状态 0未读
            personalMessage.setReceiverId("财务部");
            personalMessage.setReceiverName("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);

        }

    }

    @Override
    public List<Leverage> list() {
        QueryWrapper<Leverage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0").orderByDesc("id");
        return leverageMapper.selectList(queryWrapper);
    }

}
